﻿using IR.Common.Enums;
using IR.Common.Utilities;
using IR.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IR.Controller.ModelControllers
{
    public class CIdea
    {
        private MIdea _currentIdea;

        #region **Initializers**
        /// <summary>
        /// New Idea
        /// </summary>
        public CIdea()
        {
            _currentIdea = new MIdea();
        }

        /// <summary>
        /// For edit screens
        /// </summary>
        /// <param name="cIdeaToCopy">Push the CUser to copy</param>
        public CIdea(CIdea cIdeaToCopy)
        {
            if (cIdeaToCopy != null)
            {
                // Have to load because We can't set IdeaID
                _currentIdea = MIdea.Load(cIdeaToCopy.IdeaID);
            }
            else
            {
                _currentIdea = new MIdea();
            }
        }

        /// <summary>
        /// For existing Ideas
        /// If idea doesn't exit, it will throw an exception
        /// </summary>
        /// <param name="ideaIDToFind">Enter the ideaID to search for</param>
        public CIdea(int ideaIDToFind)
        {
            _currentIdea = MIdea.Load(ideaIDToFind);
        }

        /// <summary>
        /// Used for Controller constructors
        /// </summary>
        /// <param name="mIdeaToCopy">Pass in the MIdea to copy</param>
        internal CIdea(MIdea mIdeaToCopy)
        {
            _currentIdea = mIdeaToCopy;
        }
        #endregion

        #region **Model Relations**
        public int IdeaID
        {
            get { return _currentIdea.IdeaID; }
        }

        // Validation in MUser & CUser
        public CUser UserCreated
        {
            get { return new CUser(_currentIdea.UserCreated); }
            set
            {
                MUser temp = MUser.Load(value.Username);

                if (value != null && temp != null)
                    _currentIdea.UserCreated = temp;
            }
        }

        // Validation in MVersion & CVersion
        public CVersion Version
        {
            get { return new CVersion(_currentIdea.Version); }
            set
            {
                MVersion temp = MVersion.Load(value.VersionID);

                if (value != null && temp != null)
                    _currentIdea.Version = temp;
            }
        }

        public string Title
        {
            get { return _currentIdea.Title; }
            set
            {
                if (value != null && value.Length < 76)
                    _currentIdea.Title = value;
            }
        }

        public DateTime DateCreated
        {
            get { return _currentIdea.DateCreated; }
            set
            {
                if (value != null && value > System.DateTime.Now)
                    _currentIdea.DateCreated = value;
            }
        }

        public Status Status
        {
            get { return _currentIdea.Status; }
            set
            {
                if (value != null && (value != ((Status)0)))
                    _currentIdea.Status = value;
            }
        }

        public string Description
        {
            get { return _currentIdea.Description; }
            set { _currentIdea.Description = value; }
        }

        public DateTime? LastUpdated
        {
            get { return _currentIdea.LastUpdated; }
            set
            {
                if (value == null || value > System.DateTime.Now)
                    _currentIdea.LastUpdated = value;
            }
        }

        public CUser UserAssigned
        {
            get { return new CUser(_currentIdea.UserAssigned); }
            set
            {
                MUser temp = MUser.Load(value.Username);

                if (value != null && temp != null)
                    _currentIdea.UserAssigned = temp;
            }
        }

        public Category Category
        {
            get { return _currentIdea.Category; }
            set { _currentIdea.Category = value;}
        }
        #endregion

        /// <summary>
        /// Saves the current idea
        /// </summary>
        /// <param name="allowOverwrite">Allow editing ideas</param>
        /// <returns>True if saved</returns>
        public bool Save(bool allowOverwrite = false)
        {
            return _currentIdea.Save(allowOverwrite);
        }

        /// <summary>
        /// ***ALWAYS wrap this in a try catch... if it fails it will throw an exceiption***
        /// 
        /// Runs a query on the user table based on a Query you pass in
        /// !!!NOTE: Pass in null to get everything
        /// </summary>
        /// <param name="queries">Dictonary... key = column name, value = value in column</param>
        /// <returns>List of CIdea matching all params, all null param</returns>
        public static List<CIdea> QueryIdeas(Query queries)
        {
            // Pass in a non-null dictionary
            if (queries == null)
                queries = new Query();

            List<MIdea> modelList = MIdea.QueryIdeas(queries.Queries);

            // Return an empty List if none
            if (modelList == null)
                modelList = new List<MIdea>();

            List<CIdea> returnList = new List<CIdea>(modelList.Count);

            foreach (MIdea currentUser in modelList)
            {
                returnList.Add(new CIdea(currentUser));
            }

            return returnList;
        }
    }
}
